package com.example.noobpackage.ui.proxypackage

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.AcceptedPackage
import com.example.noobpackage.tool.ExpressIconUtil
import com.example.noobpackage.tool.StringUtil

class ProxyPackageAdapter : RecyclerView.Adapter<ProxyPackageAdapter.ViewHolder>() {

    private var packagesList: List<AcceptedPackage> = listOf()
    val onContact: (Int) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val image: ImageView = view.findViewById(R.id.typeImage)
        val location: TextView = view.findViewById(R.id.location)
        val weight: TextView = view.findViewById(R.id.weight)
        val accessCode: TextView = view.findViewById(R.id.accessingCode)
        val destination: TextView = view.findViewById(R.id.destination)
        val phoneNumber: TextView = view.findViewById(R.id.phoneNumber)
        val remark: TextView = view.findViewById(R.id.note)
        val contactBtn: Button = view.findViewById(R.id.markContact)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.accepted_package_item, parent, false
        )
        return ViewHolder(view)
    }

    fun setExpressData(newData: List<AcceptedPackage>) {
        packagesList = newData
        notifyDataSetChanged()
    }

    override fun getItemCount() = packagesList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = packagesList[position]
        holder.apply {
            image.setImageResource(ExpressIconUtil.getIconResource(data.packageType))
            location.text = data.packageLocation
            weight.text = data.weight
            accessCode.text = data.packageAccessingNumber
            destination.text = "学生公寓 C${data.department}"
            phoneNumber.text = data.telephone
            remark.text = StringUtil.ToDBC(data.remark)
            contactBtn.setOnClickListener {
                onContact(position)
            }
        }
    }
}